<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus">
          <el-option value="待审核">待审核</el-option>
          <el-option value="已驳回">已驳回</el-option>
          <el-option value="已通过">已通过</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="司机" prop="concreteCarDriver" label-width="42px">
        <el-input
          v-model.trim="queryParams.concreteCarDriver"
          placeholder="请输入司机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="牌照" prop="concreteCarLincesePlate" label-width="42px">
        <el-input
          v-model.trim="queryParams.concreteCarLincesePlate"
          placeholder="请输入牌照"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!-- 
      <el-form-item label="泵车司机" prop="pumpingCarDriver">
        <el-input
          v-model.trim="queryParams.pumpingCarDriver"
          placeholder="请输入泵车司机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="泵车牌照" prop="pumpingCarLincesePlate">
        <el-input
          v-model.trim="queryParams.pumpingCarLincesePlate"
          placeholder="请输入泵车牌照"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="false">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['service/transport-distance:audit:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['service/transport-distance:audit:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['service/transport-distance:audit:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['service/transport-distance:audit:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="auditList"
      max-height="430"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column
        label="id"
        align="center"
        prop="id"
        width="55"
        fixed="left"
      />
      <el-table-column
        label="回单时间"
        align="center"
        prop="returnOrderTime"
        min-width="160"
      >
        <template slot-scope="scope">
          {{ formatDate(scope.row.returnOrderTime) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="180"
        :show-overflow-tooltip="true"
        label="砼单编号"
        align="center"
        prop="concreteOrderNo"
      />
      <el-table-column
        min-width="180"
        :show-overflow-tooltip="true"
        label="工程名称"
        align="center"
        prop="projectName"
      />
      <el-table-column label="工程运距" align="center" prop="projectDistance" />
      <el-table-column label="实际运距" align="center" prop="actualDistance" />
      <el-table-column
        label="司机"
        align="center"
        prop="concreteCarDriver"
      />
      <el-table-column
        label="牌照"
        align="center"
        prop="concreteCarLincesePlate"
      />
      <!-- <el-table-column label="${comment}" align="center" prop="concreteCarNumber" /> -->
      <el-table-column label="运输方量" align="center" prop="transportVolume" />
      <el-table-column label="泵送方量" align="center" prop="pumpingVolum" />
      <el-table-column
        label="泵车司机"
        align="center"
        prop="pumpingCarDriver"
      />
      <el-table-column
        label="泵车牌照"
        align="center"
        prop="pumpingCarLincesePlate"
      />
      <!-- <el-table-column label="泵送员" align="center" prop="pumpingOperator" /> -->
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <!-- <template slot-scope="scope">
          <el-switch v-model="scope.row.auditStatus" disabled></el-switch>
        </template> -->
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="120"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="updateStatus(scope.row, 2)"
            v-if="scope.row.auditStatus !== '已通过'"
            >通过</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="updateStatus(scope.row, 3)"
            v-if="scope.row.auditStatus !== '已驳回'"
            >驳回</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运距审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="回单时间" prop="returnOrderTime">
          <el-input
            v-model="form.returnOrderTime"
            placeholder="请输入回单时间"
          />
        </el-form-item>
        <el-form-item label="砼单编号" prop="concreteOrderNo">
          <el-input
            v-model="form.concreteOrderNo"
            placeholder="请输入砼单编号"
          />
        </el-form-item>
        <el-form-item label="工程名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入工程名称" />
        </el-form-item>
        <el-form-item label="工程距离" prop="projectDistance">
          <el-input
            v-model="form.projectDistance"
            placeholder="请输入工程距离"
          />
        </el-form-item>
        <el-form-item label="实际距离" prop="actualDistance">
          <el-input
            v-model="form.actualDistance"
            placeholder="请输入实际距离"
          />
        </el-form-item>
        <el-form-item label="司机" prop="concreteCarDriver">
          <el-input
            v-model="form.concreteCarDriver"
            placeholder="请输入司机"
          />
        </el-form-item>
        <el-form-item label="牌照" prop="concreteCarLincesePlate">
          <el-input
            v-model="form.concreteCarLincesePlate"
            placeholder="请输入牌照"
          />
        </el-form-item>
        <el-form-item label="${comment}" prop="concreteCarNumber">
          <el-input
            v-model="form.concreteCarNumber"
            placeholder="请输入${comment}"
          />
        </el-form-item>
        <el-form-item label="运输方量" prop="transportVolume">
          <el-input
            v-model="form.transportVolume"
            placeholder="请输入运输方量"
          />
        </el-form-item>
        <el-form-item label="泵车司机" prop="pumpingCarDriver">
          <el-input
            v-model="form.pumpingCarDriver"
            placeholder="请输入泵车司机"
          />
        </el-form-item>
        <el-form-item label="泵车牌照" prop="pumpingCarLincesePlate">
          <el-input
            v-model="form.pumpingCarLincesePlate"
            placeholder="请输入泵车牌照"
          />
        </el-form-item>
        <el-form-item label="泵送方量" prop="pumpingVolum">
          <el-input v-model="form.pumpingVolum" placeholder="请输入泵送方量" />
        </el-form-item>
        <el-form-item label="泵送员" prop="pumpingOperator">
          <el-input v-model="form.pumpingOperator" placeholder="请输入泵送员" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAudit,
  getAudit,
  delAudit,
  addAudit,
  updateAudit,
  updateAuditStatus,
} from "@/api/service/distance.js";
import { formatDate } from "@/utils";
export default {
  name: "Audit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运距审核表格数据
      auditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnOrderTime: null,
        concreteOrderNo: null,
        projectName: null,
        projectDistance: null,
        actualDistance: null,
        concreteCarDriver: null,
        concreteCarLincesePlate: null,
        concreteCarNumber: null,
        transportVolume: null,
        pumpingCarDriver: null,
        pumpingCarLincesePlate: null,
        pumpingVolum: null,
        pumpingOperator: null,
        auditStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        returnOrderTime: [
          { required: true, message: "回单时间不能为空", trigger: "blur" },
        ],
        concreteOrderNo: [
          { required: true, message: "砼单编号不能为空", trigger: "blur" },
        ],
        projectName: [
          { required: true, message: "工程名称不能为空", trigger: "blur" },
        ],
        projectDistance: [
          { required: true, message: "工程距离不能为空", trigger: "blur" },
        ],
        actualDistance: [
          { required: true, message: "实际距离不能为空", trigger: "blur" },
        ],
        concreteCarNumber: [
          { required: true, message: "$comment不能为空", trigger: "blur" },
        ],
        auditStatus: [
          { required: true, message: "审核状态不能为空", trigger: "change" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatDate,
    updateStatus(row, auditStatus) {
      updateAuditStatus({ auditStatus, id: row.id }).then((res) => {
        this.$message.success("变更审核状态成功");
        this.getList();
      });
    },
    /** 查询运距审核列表 */
    getList() {
      this.loading = true;
      listAudit(this.queryParams).then((response) => {
        this.auditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        returnOrderTime: null,
        concreteOrderNo: null,
        projectName: null,
        projectDistance: null,
        actualDistance: null,
        concreteCarDriver: null,
        concreteCarLincesePlate: null,
        concreteCarNumber: null,
        transportVolume: null,
        pumpingCarDriver: null,
        pumpingCarLincesePlate: null,
        pumpingVolum: null,
        pumpingOperator: null,
        auditStatus: null,
        updateTime: null,
        updateBy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增运距审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAudit(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运距审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAudit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAudit(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除运距审核编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAudit(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "service/transport-distance/audit/export",
        {
          ...this.queryParams,
        },
        `audit_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
